package com.pay.dextest;

import android.content.Context;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public enum InjectMessageHandler {
    INSTANCE;
    private ClassLoader mClassLoader;
    private Context mContext;
    private static final String YSF_MESSAGE_CLASS = "com.unionpay.encrypt.IJniInterface";
    private static final String YSG_ENCRYPT_METHOD_NAME = "encryptMsg";
    private static final String YSG_DECRYPT_METHOD_NAME = "decryptMsg";

    /**
     * @param origin source message
     * @return decrypt info
     */
    public String decryptMessage(String origin) {
        LogUtil.log("InjectMessageHandler decryptMessage");
        checkContextInit();
        String result = null;
        try {
            Class<?> cls = mClassLoader.loadClass(YSF_MESSAGE_CLASS);
            Method method = cls.getDeclaredMethod(YSG_DECRYPT_METHOD_NAME, String.class);
            method.setAccessible(true);
            result = (String) method.invoke(cls, origin);
            LogUtil.log(String.format("decryptMessage origin = %s, result = %s", origin, result));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("ClassNotFoundException reason = " + e.getMessage());
        } catch (NoSuchMethodException e) {
            System.out.println("NoSuchMethodException reason = " + e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            System.out.println("e.printStackTrace(); reason = " + e.getMessage());
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            System.out.println("InvocationTargetException reason = " + e.getMessage());
            e.printStackTrace();
        }
        LogUtil.log("InjectMessageHandler decryptMessage result = " + result);
        return result;
    }

    /**
     * @param origin source message
     * @return encrypt info
     */
    public String encryptMessage(String origin) {
        LogUtil.log("InjectMessageHandler encryptMessage");
        checkContextInit();
        String result = null;
        try {
            Class<?> cls = mClassLoader.loadClass(YSF_MESSAGE_CLASS);
            Method method = cls.getDeclaredMethod(YSG_ENCRYPT_METHOD_NAME, String.class);
            method.setAccessible(true);
            result = (String) method.invoke(cls, origin);
            LogUtil.log(String.format("origin = %s, result = %s", origin, result));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        LogUtil.log("InjectMessageHandler encryptMessage result = " + result);
        return result;
    }

    private void checkContextInit() {
        if (null == mContext) {
            throw new ExceptionInInitializerError("context must be set!");
        }
    }

    public void init(Context context) {
        LogUtil.log("InjectMessageHandler init");
        if (null == mContext) {
            mContext = context;
            mClassLoader = mContext.getClassLoader();
        }
    }
}
